<template>
  <div>
    <a-page-header class="pt-0" @back="back" :title="t('填写表单')"></a-page-header>
    <a-spin :spinning="table.loading" class="w-[100%]">
      <a-form
        layout="vertical"
        @finish="submit"
        :rules="formRules"
        :model="form"
        ref="formRef"
        :label-col="{ span: 4 }"
        :wrapper-col="{ span: 8 }"
      >
        <a-card :bordered="false" :body-style="{ paddingTop: '0' }">
          <a-form-item :label="t('字段名')" name="fieldName">
            <a-input autofocus allow-clear v-model:value="form.fieldName" />
          </a-form-item>

          <a-form-item :label="t('字段类型')" name="fieldType">
            <a-radio-group
              :options="translateOptions(filedOptions)"
              allow-clear
              v-model:value="form.fieldType"
            />
          </a-form-item>

          <div class="flex gap-4">
            <a-button type="primary" htmlType="submit">{{ t('保存') }}</a-button>
            <a-button @click="reset">{{ t('重置') }}</a-button>
          </div>
        </a-card>
      </a-form>
    </a-spin>
  </div>
</template>

<script setup lang="ts">
import { addField, updateField } from '@/api/modules/data/field';
import { list } from '../data/curd';
import { allowEditMenu, table } from '../data/data';
import { formRef, form, formRules } from '../data/form';
const { t } = useI18n();
import { filedOptions } from '../data/options';
import { notify, translateOptions } from '@/utils/common/common';

const back = () => {
  allowEditMenu.value = false;
  reset();
};
const submit = async () => {
  let result = undefined;
  if (form.value?.fieldId) {
    const { data } = await updateField(form.value);
    result = data;
    /* 添加 */
  } else {
    /* 修改 */
    const { data } = await addField(form.value);
    result = data;
  }
  notify(result);

  await list();
  back();
};
const reset = () => {
  formRef.value?.resetFields();
};
</script>

<style lang="scss" scoped></style>
